// mongoose setup
require('../../../config');

var  Promise = require('promise'),
  assert = require('assert'),
  mailer = require('../../../../app/components/backend/service/mailer'),
  weixinDao = require('../../../../app/components/backend/db/daos/weixinuser'),
  weixinModel = require('../../../../app/components/backend/db/models/weixinuser'),
  accessTokenModel = require('../../../../app/components/backend/db/models/accesstoken'),
  util = require('../../../../app/components/backend/util'),
  config = require('../../../../app/components/backend/util').config,
  access_token = 'BYEsg8wnKAM0d_7uxOjq7E7LgTeCPBw-FQa_-NVUviAMjMSKFD-85Z7rfrJa9vLX4-g3IpDJeKh6EpAp231gofjGokYc23nC9oQ4FX_k2Cs';

var testingOpenid = 'oNaHXsspWthiBKBda-E5C_OBP_ao';
var testingEmail = 'neil.zeng@hotmail.com';

var testOptions = {openid: testingOpenid};
var testTimeoutMailToken = util.encryptor.encode({
  time: (new Date()).getTime() - config.mailTokenStayTime,
  openid: testingOpenid
});

var user_info    = {
  "subscribe"     : 1,
  "openid"        : "oNaHXsspWthiBKBda-E5C_OBP_ao",
  "nickname"      : "曾健",
  "sex"           : 1,
  "language"      : "zh_CN",
  "city"          : "大连",
  "province"      : "辽宁",
  "country"       : "中国",
  "headimgurl"    : "http:\/\/wx.qlogo.cn\/mmopen\/7y4xnsv09YZOoic8yb10tqx5rnflfUEFFbMD44F8yqGrDIcAutL4hucrDLeXCN98QQFPkyuNWgj4CpYMMwsygMnJDuplOjF5u\/0",
  "subscribe_time": 1449009999,
  "remark"        : "",
  "groupid"       : 0,
  "certificated"  : true,
  "email": "neil.zeng@tsinghua.edu.cn"
};

var testMailToken = util.encryptor.encode({
  time: (new Date()).getTime(),
  openid: testingOpenid
});

describe('app/components/backend/service/mailer', function () {
  describe('#sendMail().发送验证链接到注册邮箱', function () {
    this.timeout(5000);
    // 测试前置处理
    beforeEach(function () {

    });

    // 测试后置处理
    afterEach(function () {
      return new Promise(function (resolve, reject) {

        // 清空weixinusers collection
        weixinModel.remove({}, function (weixinErr) {
          if (weixinErr) throw new Error(weixinErr);

          // 清空accessToken collection
          accessTokenModel.remove({}, function (tokenErr) {
            if (tokenErr) throw new Error(tokenErr);
            resolve();
          });
        });
      });
    });

    it('[sendMail.subscribe.Nor.01].用户回复正确的edu邮箱，给edu邮箱发送带有验证链接的认证邮件', function () {
      return mailer.sendMail(testingEmail,testingOpenid).then(function(resData,error){
        if(resData){
          assert.ok(true,'1');
          console.log(resData);
          return Promise.resolve();
        }
        if(error){
          console.log(error);
          assert.ok(false,'发送失败');
          return Promise.reject();
        }
      });
    });

  });

  describe('#handleCallback().接受用户点击超链接验证邮箱的请求', function () {
    this.timeout(5000);
    // 测试前置处理
    beforeEach(function () {

    });

    // 测试后置处理
    afterEach(function () {
      return new Promise(function (resolve, reject) {

        // 清空weixinusers collection
        weixinModel.remove({}, function (weixinErr) {
          if (weixinErr) throw new Error(weixinErr);

          // 清空accessToken collection
          accessTokenModel.remove({}, function (tokenErr) {
            if (tokenErr) throw new Error(tokenErr);
            resolve();
          });
        });
      });
    });

    it('[handleCallback.subscribe.Nor.01].接受用户点击超链接验证邮箱的请求,验证时间未过期', function () {
      return mailer.handleCallback(testMailToken).then(function(resData,error){
          assert.equal(true,resData.certificated);
          assert.equal(null,error);

          return Promise.resolve();
      });
    });

    it('[handleCallback.subscribe.Nor.02].接受用户点击超链接验证邮箱的请求,验证时间过期', function () {

      return mailer.handleCallback(testTimeoutMailToken).then(function(resData,error){
        assert.equal("验证信息已过期，请重新获取验证信息！",resData);
        assert.equal(null,error);

        return Promise.resolve();
      });
    });

    it('[handleCallback.subscribe.Nor.03].接受用户点击超链接验证邮箱的请求,验证请求mailtoken丢失', function () {
      return mailer.handleCallback().then(function(resData,error){
        assert.equal("请关注微信后，回复邮箱获取验证链接！",resData);
        assert.equal(null,error);

        return Promise.resolve();
      });
    });

    it('[handleCallback.subscribe.Nor.04].接受用户点击超链接验证邮箱的请求,验证请求非法mailtoken', function () {
      return mailer.handleCallback().then(function(resData,error){
        assert.equal("请关注微信后，回复邮箱获取验证链接！",resData);
        assert.equal(null,error);

        return Promise.resolve();
      });
    });

  });

  describe('#sendWeixin().发送微信到验证用户', function () {
    this.timeout(5000);
    // 测试前置处理
    beforeEach(function () {

    });

    // 测试后置处理
    afterEach(function () {
      return new Promise(function (resolve, reject) {

        // 清空weixinusers collection
        weixinModel.remove({}, function (weixinErr) {
          if (weixinErr) throw new Error(weixinErr);

          // 清空accessToken collection
          accessTokenModel.remove({}, function (tokenErr) {
            if (tokenErr) throw new Error(tokenErr);
            resolve();
          });
        });
      });
    });

    it('[sendWeixin.subscribe.Nor.01].发送微信到验证用户', function () {
      return mailer.sendWeixin(testOptions).then(function(resData,error){
        if(resData){
          assert.ok(true,'1');
          console.log(resData);
          return Promise.resolve();
        }
        if(error){
          console.log(error);
          assert.ok(false,'发送失败');
          return Promise.reject();
        }
      });
    });

  });

});